package b2.scene.fight.skills
{
	import b2.scene.fight.TankSprite;
	
	public class DeathPact extends BasicSkill
	{
		public static const HEALTH_RANGE:int = 2;
		
		public function DeathPact(tankId:int, tankSide:int, level:int, skillArgs:Object)
		{
			super(tankId, tankSide ,level, skillArgs);
		}
		
		public override function checkSkillTriggerPremise(x:int, y:int, direction:int):Boolean
		{
			var tank:TankSprite = getOwner();
			if (tank.currentHp >= tank.healthPoint / HEALTH_RANGE)
				return false;
			return super.checkSkillTriggerPremise(x, y, direction);
		}
		
		public override function discharge(x:int, y:int, direction:int):void
		{
			var tempId:int = 0;
			var temprandom:Number = 0;
			for (var tankIdStr:String in getPlayState().tanks)
			{
				var targetTank:TankSprite = getPlayState().tanks[tankIdStr];
				var otherTankId:int = targetTank.tankId;
				if (otherTankId < 0 && otherTankId != tankId && targetTank.bossLevel == 0)
				{
					if (temprandom < Math.random())
					{
						tempId = otherTankId;
					}
				}
			}
			if (tempId != 0)
			{
				getPlayState().killTank(tempId, tankId);
				var effectValue:int = level < skillArgs.effectValue.length ? skillArgs.effectValue[level] : skillArgs.effectValue[0] * level;
				getPlayState().updateTankStatus(tankId, -1, getPlayState().tanks[tankId].currentHp, effectValue, 0, 0);
			}
			super.discharge(x, y, direction);
		}
	}
}